<?php
/**
 * Created by PhpStorm.
 * User: 墨染
 * Date: 2020/12/15
 * Time: 17:01
 */
function myTest($x)
{
    echo $x;
}
myTest(5);
echo PHP_EOL;


/**
 * 局部变量的作用域
 */
function test()
{
    $a = 15;
    echo "内部输出结果：" . $a;
}
echo "外部输出结果：" . $a; // 无法访问变量 a  (报错)
echo PHP_EOL;
test();
echo PHP_EOL;

/**
 * 全局变量的作用域
 */
$x = 5;
$y = 10;
$z = 0;

function test02()
{
    global $x,$y,$z;
    $z = $x + $y;
}
test02();
echo $z;
echo PHP_EOL;

/**
 * 与上相同
 */
function test03()
{
    $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
test03();
echo $z;
echo PHP_EOL;

/**
 * static作用域
 */
function test04()
{
    static $x = 0;
    echo $x . " ";
    $x++;
}

test04();
test04();
test04();
test04();
echo PHP_EOL;

/**
 * parameter 参数作用域
 */
$a = 10;
$b = 5;
function test05()
{
    $a = 15;
    $b = 5;
    $z = $a - $b;
    echo $z;
}

test05();
echo PHP_EOL;

function test06()
{
    global $a,$b;
    $a = 15;
    $b = 5;
    $z = $a-$b;
    echo $z;
}

test06();
echo PHP_EOL;

function test07()
{
    global $a,$b;
    $z= $a-$b;
    echo $z;
}

test07();

?>
